"""
架构图菜单管理工具
用于在应用安装/卸载时管理帮助菜单中的流程图菜单项
"""

import frappe

def add_architecture_menu_item():
    """
    在Navbar Settings的帮助下拉菜单中添加流程图菜单项
    """
    try:
        # 获取Navbar Settings文档
        navbar_settings = frappe.get_doc("Navbar Settings")
        
        # 检查是否已经存在流程图菜单项
        existing_item = None
        for item in navbar_settings.help_dropdown:
            if item.item_label == "流程图" or (hasattr(item, 'action') and 'show_architecture_diagrams' in str(item.action)):
                existing_item = item
                break
        
        if existing_item:
            print(f"ℹ️ 流程图菜单项已存在: {existing_item.item_label}")
            return True
        
        # 添加新的菜单项
        navbar_settings.append("help_dropdown", {
            "item_label": "流程图",
            "item_type": "Action",
            "action": "frappe.ui.misc.show_architecture_diagrams()",
            "is_standard": 0
        })
        
        # 保存文档
        navbar_settings.save()
        frappe.db.commit()
        
        print("✅ 成功添加流程图菜单项到帮助下拉菜单")
        return True
        
    except Exception as e:
        print(f"❌ 添加流程图菜单项时出错: {str(e)}")
        frappe.db.rollback()
        return False

def remove_architecture_menu_item():
    """
    从帮助下拉菜单中移除流程图菜单项
    """
    try:
        navbar_settings = frappe.get_doc("Navbar Settings")
        
        # 查找并移除流程图菜单项
        items_to_remove = []
        for i, item in enumerate(navbar_settings.help_dropdown):
            if item.item_label == "流程图" or (hasattr(item, 'action') and 'show_architecture_diagrams' in str(item.action)):
                items_to_remove.append(i)
        
        if not items_to_remove:
            print("ℹ️ 未找到流程图菜单项")
            return True
        
        # 从后往前删除，避免索引问题
        for i in reversed(items_to_remove):
            navbar_settings.help_dropdown.pop(i)
        
        navbar_settings.save()
        frappe.db.commit()
        
        print(f"✅ 成功移除 {len(items_to_remove)} 个流程图菜单项")
        return True
        
    except Exception as e:
        print(f"❌ 移除流程图菜单项时出错: {str(e)}")
        frappe.db.rollback()
        return False

def get_architecture_menu_status():
    """
    检查流程图菜单项的状态
    """
    try:
        navbar_settings = frappe.get_doc("Navbar Settings")
        
        architecture_items = []
        for item in navbar_settings.help_dropdown:
            if item.item_label == "流程图" or (hasattr(item, 'action') and 'show_architecture_diagrams' in str(item.action)):
                architecture_items.append({
                    'label': item.item_label,
                    'action': getattr(item, 'action', ''),
                    'type': item.item_type
                })
        
        return {
            'exists': len(architecture_items) > 0,
            'count': len(architecture_items),
            'items': architecture_items
        }
        
    except Exception as e:
        print(f"❌ 检查菜单状态时出错: {str(e)}")
        return {
            'exists': False,
            'count': 0,
            'items': [],
            'error': str(e)
        }